package com.gm.commonlib.util.PrefsUtils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.gm.commonlib.application.MyApplicationHolder;

/**
 * Preferences工具
 *
 * @author hck
 * @date 2017/05/15
 */
public class PrefUtils {

    private static Context getContext() {
        return MyApplicationHolder.getApplication().getContext();
    }

    public static void setString(String name,
                                 String key, String value) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                name, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
        editor.commit();
    }

    public static String getString(String name,
                                   String key) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                name, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key,"");
    }

    public static void setBoolean(String name,
                                  String key, boolean value) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                name, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static boolean getBoolean(String name,
                                     String key, boolean defaultValue) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                name, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    public static void setLong(String name,
                               String key, long value) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                name, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static long getLong(String fileName,
                               String key, long defaultValue) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                fileName, Context.MODE_PRIVATE);
        return sharedPreferences.getLong(key, defaultValue);
    }

    public static void setInt(String fileName,
                              String key, int value) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                fileName, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static int getInt(String fileName,
                             String key, int defaultValue) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                fileName, Context.MODE_PRIVATE);
        return sharedPreferences.getInt(key, defaultValue);
    }


    /**
     * 清空应用数据
     *
     * @param name    SharedPreferences文件名
     */
    public static void clearDatas(String name) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                name, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.apply();
        editor.commit();
    }

    public static void remove(String name, String key) {
        SharedPreferences sharedPreferences = getContext().getSharedPreferences(
                name, Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.remove(key).apply();
        editor.commit();
    }

}
